<template>
  <div class="login-container">
    <video src="../../assets/back.mp4" muted autoplay loop width="100vm" />

    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <!-- logo -->
      <div class="logo">
        <img src="@/assets/image/logo.png" alt="" />
      </div>

      <!-- 用户名 -->
      <el-form-item prop="account">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input v-model="loginForm.account" placeholder="请输入用户名" />
      </el-form-item>

      <!-- 密码 -->
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          v-model="loginForm.password"
          placeholder="请输入密码"
          show-password
        />
      </el-form-item>

      <!-- 验证码 -->
      <el-form-item prop="code">
        <div class="validate_code">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input v-model="loginForm.code" placeholder="验证码" />
          <img :src="imgUrl" alt="" @click="changeImgCode" />
        </div>
      </el-form-item>

      <el-button
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click="login"
        >登录</el-button
      >
    </el-form>
  </div>
</template>

<script>
import { getImgCode } from "@/api/user";
import { throttle } from "lodash";
export default {
  name: "Login",
  data() {
    return {
      imgUrl: "",
      loginForm: {
        key: "",
        account: process.env.NODE_ENV === "development" ? "demo" : "",
        password: process.env.NODE_ENV === "development" ? "hm#qd@23!" : "",
        code: "",
      },
      loginRules: {
        account: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个字符",
            trigger: "blur",
          },
        ],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getImgCode();
  },
  methods: {
    // 获取图片验证码
    async getImgCode() {
      try {
        this.loginForm.key = Math.random();
        const res = await getImgCode({ key: this.loginForm.key });
        // 转换为base64 图片字符串
        const fr = new FileReader();
        fr.onload = (e) => {
          this.imgUrl = e.target.result;
        };
        fr.readAsDataURL(res);
      } catch (error) {
        console.log(error);
      }
    },

    // 切换验证码 防抖
    changeImgCode: throttle(function () {
      this.getImgCode();
    }, 2000),

    // 登录
    async login() {
      try {
        // 校验表单
        await this.$refs.loginForm.validate();
        // 请求登录
        await this.$store.dispatch("user/login", this.loginForm);
        this.$router.push(this.$route.query.redirect || "/"); // 跳转到记录/首页
      } catch (error) {
        this.getImgCode();
      }
    },
  },
};
</script>

<style lang="scss">
$bg: #283443;
$light_gray: #b3b3b3;
$cursor: #333;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(0, 0, 0, 0.1);
    background: transparent;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  position: absolute;
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    background-color: #fff;
    top: 180px;
    width: 520px;
    max-width: 100%;
    padding: 36px 40px;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>

<style lang="scss" scoped>
.logo {
  width: 320px;
  margin: 0 50px 30px;
  img {
    width: 100%;
  }
}

video {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  z-index: 0;
}

.validate_code {
  position: relative;
  img {
    position: absolute;
    top: 5px;
    right: 5px;
  }
}
</style>
